package com.judian.smart.util;

import java.util.Timer;
import java.util.TimerTask;

/**
 * author: Peter
 * date: 2019/10/12 星期六 13:49
 * desc:
 */
public class TimerUtil extends Timer {
    private TimerTask task;
    private long time;
    private Timer timer;

    TimerUtil(TimerTask task, long time) {
        this.task = task;
        this.time = time;
    }

    /**
     * 开始定时任务
     */
    public void start() {
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(task, time);
    }

    public void start(long time) {
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(task, time);
    }

    /**
     * 重复任务
     * @param delay 在等待delay毫秒后首次执行task
     * @param period 每隔period毫秒重复执行task
     */
    public void scheduleRepeat(long delay,long period){
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(task, delay, period);
    }

    /**
     * 销毁定时任务
     */
    public void cancel() {
        if (task != null) {
            task.cancel();
        }
    }
}
